import json
from urllib import request
from flask import Flask, jsonify
import db

app = Flask(__name__)
#接口
@app.route('/couponM',methods=['POST',"GET"])
    #添加
def couponM():
    if request.method == "POST":
        #获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        #构造sql语句
        sql = "insert into coupon values(0,'%s',%d,'%s','%s')"%(data['name'],int(data['total']),data['stime'],data['etime'])
        #pymysql
        db.insert(sql)
        #返回结果
        return 'ok'
    elif request.method == 'PUT':
        # 修改
        # 获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        # 构造sql语句 update 表名 set 字段名1=值，字段名2=值 where id=1
        sql = "update coupon set name='%s',total=%d,starttime='%s',endtime='%s' where id=%d"%
        (data['name'],int(data['total']),data['stime'],data['etime'],int(data['id']))
        db.insert(sql)
        return jsonify({"code":200})

if __name__=="__main__":
    app.run(host='     127.0.0.1',port=5001)